package com.paul.ddyw.controller.back;

import com.paul.exception.BusinessException;
import com.paul.sys.tools.PageData;
import com.paul.sys.util.ParamUtil;
import com.paul.sys.tools.PrimaryGenerater;
import com.paul.ddyw.model.Order;
import com.paul.ddyw.service.OrderService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by yangjianbo on 2016/2/17.
 */
@Controller
@RequestMapping("back/order/")
public class OrderController {
    private static Logger logger = Logger.getLogger(OrderController.class);
    @Autowired
    OrderService orderService;
    /**
     * 返回菜单管理视图
     * @return
     * @throws Exception
     */
    @RequestMapping("ismenu/order")
    public String orderList() throws Exception {
        return "back/ddyw/order-list";
    }
    @RequestMapping("ismenu/orderlist1")
    public String orderList1() throws Exception {
        return "back/ddyw/order-list1";
    }
    @RequestMapping("ismenu/orderlist2")
    public String orderList2() throws Exception {
        return "back/ddyw/order-list2";
    }
    @RequestMapping("ismenu/orderlist3")
    public String orderList3() throws Exception {
        return "back/ddyw/order-list3";
    }
    @RequestMapping("ismenu/orderlist4")
    public String orderList4() throws Exception {
        return "back/ddyw/order-list4";
    }
    @RequestMapping("ismenu/orderlist5")
    public String orderList5() throws Exception {
        return "back/ddyw/order-list5";
    }
    @RequestMapping("ismenu/orderlist11")
    public String orderList11() throws Exception {
        return "back/ddyw/order-list-1";
    }

    @RequestMapping("add_page")
    public String add_page(){
        return "back/ddyw/order-add";
    }
    @RequestMapping("edit_page")
    public ModelAndView edit_page(String uuid){
        ModelAndView modelAndView=null;
        modelAndView = new ModelAndView("back/ddyw/order-edit");
        Order order=null;
        if(uuid!=null){
            order=this.orderService.selectByPrimaryKey(uuid);
            modelAndView.addObject(order);
        }
        return modelAndView;
    }
    @ResponseBody
    @RequestMapping("list")
    public Map<String, Object> listPtstaff(HttpServletRequest request) throws Exception{

        Map<String, Object> result = new HashMap<String, Object>();
        if(result!=null){
//            throw new RuntimeException("qqq");
        }
        try {
            ParamUtil util = new ParamUtil(request);
            Map<String, String> param = util.getParamMap();
            PageData pageData = orderService.listPage(param);
            result.put("totalRows", pageData.getPageBean().getTotalNum());
            result.put("curPage", param.get("curPage"));
            result.put("data", pageData.getListData());
            result.put("success", true);
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("doadd")
    public Map orderSave(Order order) throws  Exception{
        Map<String, Object> result = new HashMap<String, Object>();
        String id= PrimaryGenerater.getInstance().generaterNextNumber();
        if(order!=null){
            order.setUuid(id);
            order.setDate(new Date());
            order.setStatus("1");
            int n=this.orderService.placeOrder(order);
            if(n==1){
                result.put("status",0);
            }
//            throw new BusinessException("01","订单错误");
        }
        else{
            result.put("status",1);
            result.put("msg","插入失败");
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("docancel")
    public Map orderCancel(String uuid){
        Map<String, Object> result = new HashMap<String, Object>();
        boolean n=this.orderService.cancelOrder(uuid);
        if(n==true){
            result.put("status",0);
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("confirm")
    public Map confirmOrder(String uuid){
        Map<String, Object> result = new HashMap<String, Object>();
        boolean n=this.orderService.confirmOrder(uuid);
        if(n==true){
            result.put("status",0);
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("code")
    public Map codeOrder(String uuid){
        Map<String, Object> result = new HashMap<String, Object>();
        boolean n=this.orderService.codeOrder(uuid);
        if(n==true){
            result.put("status",0);
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("recive")
    public Map reciveOrder(String uuid){
        Map<String, Object> result = new HashMap<String, Object>();
        boolean n=this.orderService.reciveOrder(uuid);
        if(n==true){
            result.put("status",0);
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("pay")
    public Map payOrder(String uuid){
        Map<String, Object> result = new HashMap<String, Object>();
        boolean n=this.orderService.payOrder(uuid);
        if(n==true){
            result.put("status",0);
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("complate")
    public Map complateOrder(String uuid){
        Map<String, Object> result = new HashMap<String, Object>();
        boolean n=this.orderService.complateOrder(uuid);
        if(n==true){
            result.put("status",0);
        }
        return result;
    }
//    @ExceptionHandler(RuntimeException.class)
//    public void operateExpAjax(RuntimeException ex){
//       System.out.println("拦截到异常信息。。。。。。。。。。。。。。。。。。。。。。。。。。。。");
//    }
}
